
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Random;
import  java.io.IOException;
public class exercise_3 {
        static public void main(String[] args)throws IOException, InterruptedIOException {
            InputStreamReader reader = new InputStreamReader(new FileInputStream("E:\\exercise\\src\\name.txt"), "UTF8");
            BufferedReader read1 = new BufferedReader(reader);
            InputStreamReader reader2 = new InputStreamReader(new FileInputStream("E:\\exercise\\src\\score.txt"), "UTF8");
            BufferedReader read2 = new BufferedReader(reader2);
            OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("E:\\exercise\\src\\unlined.txt", true));
            BufferedWriter bw = new BufferedWriter(writer);
            OutputStreamWriter writer2 = new OutputStreamWriter(new FileOutputStream("E:\\exercise\\src\\lined.txt", true));
            BufferedWriter sortedbw = new BufferedWriter(writer2);

            ArrayList students = new ArrayList();
            int[] score = new int[6];
            int stuNumber = 0;
            String head = read2.readLine();
            bw.write(head);
            String line;
            while ((line=read2.readLine())!=null) {
                bw.newLine();
                bw.write(line);
                stuNumber++;
                String[] score_info = line.split(",");
                String content = line.substring(0, line.lastIndexOf(','));
                ArrayList sumgrade = new ArrayList();
                sumgrade.add(content);
                int sum = Integer.parseInt(score_info[7]);
                for (int i = 0; i < 5; i++) {
                    score[i] += Integer.parseInt(score_info[i + 2]);
                }
                score[5] = sum;
                sumgrade.add(sum);
                students.add(sumgrade);
            }
            read2.close();

            line=read1.readLine();
            while((line=read1.readLine())!=null){
            int sum=0;
                String nextdata=line;
                for(int i=0;i<5;i++){
                    int sco=(int)(75+new Random().nextGaussian()*25);
                    if (sco>=100)sco=100;else if (sco<=50)sco=50;
                    sum+=sco;
                    score[i]+=sco;
                    nextdata +=","+sco;
                }
                score[5]+=sum;
                ArrayList sumgrade=new ArrayList();
                sumgrade.add(nextdata);
                sumgrade.add(sum);
                students.add(sumgrade);
                nextdata+=","+sum;
                bw.newLine();
                bw.write(nextdata);
                stuNumber++;
            }
            read1.close();
            String nextdata="平均分,";
            for(int i=0;i<score.length;++i){
                nextdata+=","+(score[i]/stuNumber);
            }
            bw.newLine();
            bw.write(nextdata);
            ArrayList[] students_array = (ArrayList[])students.toArray(new ArrayList[stuNumber]);
            sortedbw.write(head);
            mergeSort(students_array,0,students_array.length-1);
            for(Object student:students_array){
                String content = (String)((ArrayList)student).get(0)+","+(int)(((ArrayList)student).get(1));
                sortedbw.newLine();
                sortedbw.write(content);
            }
            bw.flush();
            bw.close();
            sortedbw.flush();
            sortedbw.close();
        }


    static private ArrayList[] mergeSort(ArrayList[]students, int left, int right){

        int center = (left+right)/2;
        if(left<right){
            mergeSort(students,left,center);
            mergeSort(students,center+1,right);
            merge(students,left,center,right);
        }
        return students;
    }
    static private void merge(ArrayList[]A,int lp,int cp,int rp){
        ArrayList[] temp = new ArrayList[rp-lp+1];

        int lptr = lp;
        int rptr = cp+1;
        int k = 0;

        while(lptr<=cp && rptr <= rp){

            if((int)(A[lptr].get(1)) >= (int)(A[rptr].get(1))){
                temp[k++] = A[lptr++];
            }else {
                temp[k++] = A[rptr++];
            }

        }
        while(lptr<=cp){
            temp[k++] = A[lptr++];
        }
        while (rptr<=rp){
            temp[k++] = A[rptr++];
        }

        for(int i =0;i<rp-lp+1;i++){
            A[i+lp] = temp[i];
        }
    }
}